﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OA.Mvc3.Controllers
{
    [ValidateInput(false)]
    public class IPBlackWhiteController :UI.BaseController
    {
        public const string ProgrameCode = "IPBlackWhite";
        public static readonly string currentPath = UI.Utils.GetCurrentPath(ProgrameCode);

        public IPBlackWhiteController()
        {
            ViewBag.CurrentPath = currentPath;
        }

        [UI.ValidateAuthority(ProgrameCode)]     
        public ActionResult Index()
        {
            return View();
        }

        [UI.ValidateAuthority(ProgrameCode)]     
        public string List(int page, int rows, string sort, string order)
        {
            string where = Common.HqlQeuryHelp.GetWhere(typeof(Model.IPBlackWhite), "Type,BeginIP,EndIP");
            string orderby=null;
            if (!string.IsNullOrEmpty(sort)) orderby = sort + " " + order;

            var bll = new BLL.IPBlackWhite();
            var list = bll.Get(where,orderby , (page - 1) * rows, rows);
            List<object> listView = new List<object>();
            foreach (var i in list)
            {
                listView.Add(new
                {
                    id = i.ID,
                    Type=i.Type,
                    BeginIP = i.BeginIP,
                    EndIP = i.EndIP,
                    Status=i.Status,
                    Memo=i.Memo
                });
            }
            var ret = new
            {
                total = bll.GetCount(where),
                rows = listView
            };
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "delete")]     
        public string Delete(string id)
        {
            var ret = new BLL.IPBlackWhite().Delete(id);
            return UI.Utils.ToJsonString(ret);
        }

        [UI.ValidateAuthority(ProgrameCode , "add")]     
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "add")]     
        public string Add(Model.IPBlackWhite model)
        {
            model.BeginIP = Common.Utils.FormatIP(model.BeginIP);
            model.EndIP = Common.Utils.FormatIP(model.EndIP);
            var ret = new BLL.IPBlackWhite().Add(model);
            return UI.Utils.ToJsonString(ret);
            
        }

        [UI.ValidateAuthority(ProgrameCode , "edit")]     
        public ActionResult Edit(int id)
        {
            var model = new BLL.IPBlackWhite().Get(id);
            if (model == null) return UI.Utils.ErrorDateNoExist();
            return View(model);
        }

        [HttpPost]
        [UI.ValidateAuthority(ProgrameCode , "edit")]     
        public string Edit(Model.IPBlackWhite model)
        {
            model.BeginIP = Common.Utils.FormatIP(model.BeginIP);
            model.EndIP = Common.Utils.FormatIP(model.EndIP);
            var ret = new BLL.IPBlackWhite().Update(model);
            return UI.Utils.ToJsonString(ret);
        }
    }
}
